package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)

func readFile() {
	const filename = "abc.txt"
	contents, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Printf("读取文件报错:\n %s\n", err)
	}else {
		fmt.Printf("%s\n", contents)
	}
}

func grade(score int) string {
	res := ""
	switch {
	case score < 0 || score > 100:
		panic(fmt.Sprintf("wrong score: %d", score))
	case score < 60:
		res = "C"
	case score < 80:
		res = "B"
	case score <= 100:
		res = "A"
	}
	return res
}

/**
 * 普通循环
 */
func coverToBin(a int) string {
	res := "";
	for ; a > 0; a /= 2  {
		res = strconv.Itoa(a % 2) + res
	}
	return res
}

/**
 *循环不写初始条件和结束条件 相当于java中的while
 */
func printFile(filename string)  {
	file, err := os.Open(filename)
	if err != nil {
		panic(fmt.Sprintf("open file error: \n %s", err))
	}
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}

/**
 *死循环
 */
func foreverLoop() {
	i := 0
	for {
		i = i + 1
		fmt.Printf("死循环: %d \n", i)
	}
}

func main() {
	readFile()
	fmt.Println(grade(0))
	fmt.Println(grade(20))
	fmt.Println(grade(65))
	fmt.Println(grade(90))
	//fmt.Println(grade(101))
	fmt.Println(coverToBin(1))
	fmt.Println(coverToBin(4))
	fmt.Println(coverToBin(100))
	printFile("abc.txt")
	//foreverLoop()
}
